package com.mls.guardian;


import com.mls.guardian.config.ProjectConfig;
import com.mls.guardian.dataobj.*;
import com.mls.guardian.em.ModuleType;
import com.mls.guardian.protocol.CodeType;
import com.mls.guardian.protocol.RemoteBody;
import com.mls.guardian.protocol.RemoteCommand;
import com.mls.guardian.protocol.body.*;
import com.mls.guardian.remoting.netty.NettyClient;
import com.mls.guardian.remoting.netty.RemoteTools;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by jl on 15/9/11.
 */
public class RestartApp {
    public static void main(String a[]) throws Exception {
//        updateStaticFile();
//        getBackup();
        startApp();
//        restartApp();
//        getProcess();
//        restoreApp();
//        stopApp();
//        serverInfo();
//        serverProcessInfo();
//        getFileInfo();
//        upgrade();
//        getUpgradeVersion();
    }


    public static void serverProcessInfo() throws Exception {
        NettyClient client = new NettyClient(null);
        ServerProcessInfoBody body = new ServerProcessInfoBody();
        body.setProjectName("wms");


        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.SERVER_PROCESS_INFO, body);
        RemoteCommand response = client.sendMessageSync("localhost:18091", request, -1);


        ServerProcessInfoBody responseBody = (ServerProcessInfoBody) response.getRemoteBody();
        System.out.println(responseBody.getProcessInfoList());
    }

    public static void serverInfo() throws Exception {
        NettyClient client = new NettyClient(null);

        ServerInfoBody serverInfo = new ServerInfoBody();

        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.SERVER_INFO, serverInfo);
        RemoteCommand response = client.sendMessageSync("localhost:18091", request, -1);
        RemoteBody appRestartRes = response.getRemoteBody();
        System.out.println("执行结果 " + appRestartRes.isSuc());

        if (!appRestartRes.isSuc()) {
            System.out.println("error message " + ((RemoteBody) appRestartRes).getErrorContent());

        } else {
            ServerInfoBody serverInfoRes = (ServerInfoBody) response.getRemoteBody();
            for (ServerInfo str : serverInfoRes.getServerList()) {
                System.out.println("server is " + str.getIp() + "====" + str.getRole());
            }
        }

        client.shutdown();
    }


    public static void getBackup() throws Exception {
        NettyClient client = new NettyClient(null);

        BackupInfoBody backupInfoBody = new BackupInfoBody();
        backupInfoBody.setProjectName("wms");

        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.GET_BACKUP, backupInfoBody);
        RemoteCommand response = client.sendMessageSync("localhost:9999", request, -1);
        RemoteBody remoteBody = response.getRemoteBody();
        System.out.println("执行结果 " + remoteBody.isSuc() + ";error:" + remoteBody.getErrorContent());
        if (remoteBody.isSuc()) {
            BackupInfoBody rtn = (BackupInfoBody) remoteBody;
            for (BackupServerObj backupServerObj : rtn.getBackupServerObjList()) {
                System.out.println("server ip:" + backupServerObj.getIp());
                for (BackupProjectObj backupProjectObj : backupServerObj.getList()) {
                    System.out.println("projectName:" + backupProjectObj.getProjectName() + ";versionTag:" + backupProjectObj.getVersionTag());
                    for (BackupModuleObj backupModuleObj : backupProjectObj.getModuleObjList()) {
                        System.out.println("moduleName:" + backupModuleObj.getModuleName() + ";backupTime:" + backupModuleObj.getBackupTime());
                    }
                }
            }
        }

        if (!remoteBody.isSuc()) {
            if (response.getCode() == CodeType.SYSTEM_ERROR) {
                System.out.println("error message " + ((RemoteBody) remoteBody).getErrorContent());
            }

        }
    }

    public static void getProcess() throws Exception {
        NettyClient client = new NettyClient(null);

        ServerProcessInfoBody serverProcessInfoBody = new ServerProcessInfoBody();
        serverProcessInfoBody.setProjectName("wms");

        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.SERVER_PROCESS_INFO, serverProcessInfoBody);
        RemoteCommand response = client.sendMessageSync("192.168.113.18:18091", request, -1);

        RemoteBody appRestartRes = response.getRemoteBody();
        System.out.println("执行结果 " + appRestartRes.isSuc() + ";error:" + appRestartRes.getErrorContent());
        if (!appRestartRes.isSuc()) {
            if (response.getCode() == CodeType.SYSTEM_ERROR) {
                System.out.println("error message " + ((RemoteBody) appRestartRes).getErrorContent());
            }
        } else {
            ServerProcessInfoBody rtn = (ServerProcessInfoBody) appRestartRes;
            for (ServerProcessInfo serverInfo : rtn.getProcessInfoList()) {
                System.out.println("server ip:" + serverInfo.getIp());
                for (ProcessMessage item : serverInfo.getProcessList()) {
                    System.out.println("pid:" + item.getProcessId() + ";pname:" + item.getModuleName() + ":isRun:" + item.isRun() + ";processType:" + item.getProcessType());
                }
            }
        }
    }

    public static void startApp() throws Exception {
        NettyClient client = new NettyClient(null);

        AppRestartBody appRestart = new AppRestartBody();
        appRestart.setProjectName("wms");

        List<ProcessMessage> processObjList = new ArrayList<ProcessMessage>();
        processObjList.add(new ProcessMessage("0", "MainComServer"));
//        processObjList.add(new ProcessMessage("0", "SkuServer"));
//        processObjList.add(new ProcessMessage("0", "InWarehouseServer"));
//        processObjList.add(new ProcessMessage("0", "OutWarehouseServer"));
//        processObjList.add(new ProcessMessage("0", "WmsMQ"));
//        processObjList.add(new ProcessMessage("0", "AppUserControl"));
//        processObjList.add(new ProcessMessage("0", "WmsApi"));
//        processObjList.add(new ProcessMessage("0", "MobileApp"));
//        processObjList.add(new ProcessMessage("0", "TimingServer"));
//        processObjList.add(new ProcessMessage("0", "AppWayBill"));

        List<ServerProcessInfo> serverProcessList = new ArrayList<ServerProcessInfo>();
        ServerProcessInfo serverProcessInfo = new ServerProcessInfo();
        serverProcessInfo.setIp("192.168.116.107");
        serverProcessInfo.setProcessList(processObjList);
        serverProcessList.add(serverProcessInfo);

        appRestart.setServerProcessInfoListList(serverProcessList);

        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.RESTART, appRestart);
        RemoteCommand response = client.sendMessageSync("127.0.0.1:9999", request, -1);

        RemoteBody remoteBody = response.getRemoteBody();
        System.out.println("error message " + remoteBody.isSuc() + remoteBody.getErrorContent());
        if (!remoteBody.isSuc()) {
            if (response.getCode() == CodeType.SYSTEM_ERROR) {
                System.out.println("error message " + remoteBody.getErrorContent());
            }
        } else {
            AppRestartBody appRestartRes = (AppRestartBody) response.getRemoteBody();
            System.out.println("执行结果 " + appRestartRes.isSuc() + ";error:" + appRestartRes.getErrorContent()
                    + ";versionTag:" + appRestartRes.getVersionTag() + ";versionSvn:" + appRestartRes.getVersion());
        }
    }

    public static void restartApp() throws Exception {
        final String DATE_FORMAT = "yyyyMMdd_HH-mm-ss-SSS";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);

        NettyClient client = new NettyClient(null);

        AppRestartBody appRestart = new AppRestartBody();
        appRestart.setProjectName("wms");
        appRestart.setVersionTag(simpleDateFormat.format(new Date(System.currentTimeMillis())));

        List<ProcessMessage> processObjList = new ArrayList<ProcessMessage>();
        processObjList.add(new ProcessMessage("0", "MainComServer"));
        processObjList.add(new ProcessMessage("0", "SkuServer"));
        processObjList.add(new ProcessMessage("0", "InWarehouseServer"));
        processObjList.add(new ProcessMessage("0", "OutWarehouseServer"));
        processObjList.add(new ProcessMessage("0", "WmsMQ"));
        processObjList.add(new ProcessMessage("0", "AppUserControl"));
        processObjList.add(new ProcessMessage("0", "WmsApi"));
        processObjList.add(new ProcessMessage("0", "MobileApp"));
        processObjList.add(new ProcessMessage("0", "TimingServer"));
        processObjList.add(new ProcessMessage("0", "AppWayBill"));

        List<ServerProcessInfo> serverProcessList = new ArrayList<ServerProcessInfo>();
        ServerProcessInfo serverProcessInfo = new ServerProcessInfo();
        serverProcessInfo.setIp("192.168.113.18");
        serverProcessInfo.setProcessList(processObjList);
        serverProcessList.add(serverProcessInfo);

        appRestart.setServerProcessInfoListList(serverProcessList);

        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.RESTART, appRestart);
        RemoteCommand response = client.sendMessageSync("192.168.113.18:18091", request, -1);

        RemoteBody remoteBody = response.getRemoteBody();
        System.out.println("error message " + remoteBody.isSuc() + remoteBody.getErrorContent());
        if (!remoteBody.isSuc()) {
            if (response.getCode() == CodeType.SYSTEM_ERROR) {
                System.out.println("error message " + remoteBody.getErrorContent());
            }
        } else {
            AppRestartBody appRestartRes = (AppRestartBody) response.getRemoteBody();
            System.out.println("执行结果 " + appRestartRes.isSuc() + ";error:" + appRestartRes.getErrorContent()
                    + ";versionTag:" + appRestartRes.getVersionTag() + ";versionSvn:" + appRestartRes.getVersion());
        }
    }


    public static void updateStaticFile() throws Exception {
        NettyClient client = new NettyClient(null);

        AppRestartBody appRestart = new AppRestartBody();
        appRestart.setProjectName("wms");

        //更新单个文件
        List<String> fileList = new ArrayList<String>();
        fileList.add("_login.html");
        fileList.add("html/说明.txt");

        //进程模块信息
        ProcessMessage processMessage = new ProcessMessage("0", "AppUserControl");
        processMessage.setModuleFile(fileList);

        List<ProcessMessage> processObjList = new ArrayList<ProcessMessage>();
        processObjList.add(processMessage);

        //服务器进程信息
        ServerProcessInfo serverInfo = new ServerProcessInfo();
        serverInfo.setIp("192.168.113.18");
        serverInfo.setProcessList(processObjList);

        List<ServerProcessInfo> processInfoList = new ArrayList<ServerProcessInfo>();
        processInfoList.add(serverInfo);

        appRestart.setServerProcessInfoListList(processInfoList);

        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.UPDATE_STATIC_FILE, appRestart);
        RemoteCommand response = client.sendMessageSync("192.168.113.18:18091", request, -1);
        RemoteBody appRestartRes = response.getRemoteBody();
        System.out.println("执行结果 " + appRestartRes.isSuc() + ";error:" + appRestartRes.getErrorContent());

        if (!appRestartRes.isSuc()) {
            if (response.getCode() == CodeType.SYSTEM_ERROR) {
                System.out.println("error message " + ((RemoteBody) appRestartRes).getErrorContent());
            }

        }
    }

    public static void restoreApp() throws Exception {
        NettyClient client = new NettyClient(null);

        AppRestartBody appRestore = new AppRestartBody();
        appRestore.setProjectName("wms");
        appRestore.setVersionTag("20151117_14-21-51-746");//还原版本号


        List<ServerInfo> hostList = new ArrayList<ServerInfo>();

        ServerInfo serverInfo = new ServerInfo();
        serverInfo.setIp("192.168.113.30");
//        serverInfo.setProcessObjList(null);

        hostList.add(serverInfo);

//        appRestore.setServerList(hostList);
        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.RESTORE, appRestore);
        RemoteCommand response = client.sendMessageSync("localhost:9999", request, -1);
        RemoteBody appRestartRes = response.getRemoteBody();
        System.out.println("执行结果 " + appRestartRes.isSuc() + ";error:" + appRestartRes.getErrorContent());

        if (!appRestartRes.isSuc()) {
            if (response.getCode() == CodeType.SYSTEM_ERROR) {
                System.out.println("error message " + ((RemoteBody) appRestartRes).getErrorContent());
            }

        }
    }

    public static void stopApp() throws Exception {
        NettyClient client = new NettyClient(null);

        AppRestartBody appRestart = new AppRestartBody();
        appRestart.setProjectName("wms");

        List<ProcessMessage> processObjList = new ArrayList<ProcessMessage>();
        processObjList.add(new ProcessMessage("0", "BaseComServer"));
//        processObjList.add(new ProcessMessage("0", "MainComServer"));
//        processObjList.add(new ProcessMessage("0", "SkuServer"));
//        processObjList.add(new ProcessMessage("0", "InWarehouseServer"));
//        processObjList.add(new ProcessMessage("0", "OutWarehouseServer"));
//        processObjList.add(new ProcessMessage("0", "WmsMQ"));
//        processObjList.add(new ProcessMessage("0", "AppUserControl"));
//        processObjList.add(new ProcessMessage("0", "WmsApi"));
//        processObjList.add(new ProcessMessage("0", "MobileApp"));
//        processObjList.add(new ProcessMessage("0", "TimingServer"));
//        processObjList.add(new ProcessMessage("0", "AppWayBill"));

        List<ServerProcessInfo> serverProcessList = new ArrayList<ServerProcessInfo>();
        ServerProcessInfo serverProcessInfo = new ServerProcessInfo();
        serverProcessInfo.setIp("192.168.113.18");
        serverProcessInfo.setProcessList(processObjList);
        serverProcessList.add(serverProcessInfo);

        appRestart.setServerProcessInfoListList(serverProcessList);

        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.STOP, appRestart);
        RemoteCommand response = client.sendMessageSync("192.168.113.18:18091", request, -1);


        RemoteBody remoteBody = response.getRemoteBody();
        if (!remoteBody.isSuc()) {
            if (response.getCode() == CodeType.SYSTEM_ERROR) {
                System.out.println("error message " + remoteBody.getErrorContent());
            }
        } else {
            AppRestartBody appRestartRes = (AppRestartBody) remoteBody;
            System.out.println("执行结果 " + appRestartRes.isSuc() + ";error:" + appRestartRes.getErrorContent());
        }
    }

    public static void getFileInfo() throws Exception {
        NettyClient client = new NettyClient(null);

        FileInfoBody fileInfoBody = new FileInfoBody();
        fileInfoBody.setIpAddress("192.168.113.18");
        fileInfoBody.setProjectName("wms");
        fileInfoBody.setModuleName("AppUserControl");
        fileInfoBody.setParent("app/scripts");

        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.GET_FILE_INFO, fileInfoBody);
        RemoteCommand response = client.sendMessageSync("192.168.113.18:18091", request, -1);
        RemoteBody remoteBody = response.getRemoteBody();
        System.out.println("执行结果 " + remoteBody.isSuc() + ";error:" + remoteBody.getErrorContent());
        if (remoteBody.isSuc()) {
            FileInfoBody rtn = (FileInfoBody) remoteBody;
            for (FileObj item : rtn.getChildren()) {
                System.out.println("FileName:" + item.getFileName() + ";FilePath:" + item.getFilePath() + ";FileType:" + item.getFileType());
            }

        }

        if (!remoteBody.isSuc()) {
            if (response.getCode() == CodeType.SYSTEM_ERROR) {
                System.out.println("error message " + ((RemoteBody) remoteBody).getErrorContent());
            }

        }
    }

    /**
     * 自我更新
     *
     * @throws Exception
     */
    public static void upgrade() throws Exception {
        NettyClient client = new NettyClient(null);

        AppRestartBody requestBody = new AppRestartBody();

        List<ServerProcessInfo> serverProcessInfoList = new ArrayList<ServerProcessInfo>();

        ServerProcessInfo serverProcessInfo = new ServerProcessInfo();
        serverProcessInfo.setIp("192.168.113.18");
        serverProcessInfoList.add(serverProcessInfo);

        serverProcessInfo = new ServerProcessInfo();
        serverProcessInfo.setIp("192.168.113.125");
        serverProcessInfoList.add(serverProcessInfo);

        requestBody.setServerProcessInfoListList(serverProcessInfoList);

        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.UPGRADE, requestBody);
        client.sendMessageSync("192.168.113.18:18091", request, -1);
    }

    /**
     * 获取自我更新版本
     *
     * @throws Exception
     */
    public static void getUpgradeVersion() throws Exception {
        NettyClient client = new NettyClient(null);

        ServerUpgradeInfoBody requestBody = new ServerUpgradeInfoBody();

        List<ServerUpgradeInfo> serverUpgradeInfoList = new ArrayList<ServerUpgradeInfo>();

        ServerUpgradeInfo serverUpgradeInfo = new ServerUpgradeInfo();
        serverUpgradeInfo.setIp("192.168.113.18");
        serverUpgradeInfoList.add(serverUpgradeInfo);


        serverUpgradeInfo = new ServerUpgradeInfo();
        serverUpgradeInfo.setIp("192.168.113.125");
        serverUpgradeInfoList.add(serverUpgradeInfo);

        requestBody.setUpgradeInfoList(serverUpgradeInfoList);

        RemoteCommand request = RemoteTools.getRequestRemoteCmd(CodeType.GET_UPGRADE_VERSION, requestBody);
        RemoteCommand response = client.sendMessageSync("192.168.113.18:18091", request, -1);

        RemoteBody remoteBody = response.getRemoteBody();
        System.out.println("执行结果 " + remoteBody.isSuc() + ";error:" + remoteBody.getErrorContent());
        if (remoteBody.isSuc()) {
            ServerUpgradeInfoBody rtn = (ServerUpgradeInfoBody) remoteBody;
            for (ServerUpgradeInfo item : rtn.getUpgradeInfoList()) {
                System.out.println("IPAddress:" + item.getIp() + ",Version:" + item.getVersion());
            }

        }
    }
}
